package com.scs.application.modules.dept.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.dept.entity.HisBill;
import com.scs.application.modules.dept.service.HisBillService;
import com.scs.application.modules.report.handler.BeforeReport;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.thirdparty.request.dto.HisReqBill;
import com.scs.application.modules.thirdparty.request.view.his.HisReqServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/his_bill")
public class HisBillController extends CrudController<HisBillService, HisBill>{
    @Autowired
    HisReqServiceImpl hisReqViewService;
    /**
     * @desc SN后补扫码消耗
     */
    @GetMapping("/scanUseSN")
    public RestResult scanUseSN(String matrCodeHis,String inpatientCode,String sn, String opType, String hisKey, String outpatientNo, String oprtNo) {
        if (StringUtils.isBlank(sn)) return RestResult.error("操作失败,缺少参数【sn】");
        if (StringUtils.isBlank(opType)) return RestResult.error("操作失败,缺少参数【opType】");
        if(StringUtils.isBlank(hisKey)) return RestResult.error("操作失败,缺少参数【hisKey】");
        if (!"bind,unbind".contains(opType)) return RestResult.error("操作失败,参数【opType】非法");
        if(StringUtils.isBlank(inpatientCode)&&StringUtils.isBlank(outpatientNo)&&StringUtils.isBlank(oprtNo)) {
            return RestResult.error("操作失败,参数【inpatientCode】,【outpatientNo】,【oprtNo】均为空");
        }
        String opTypeName = "解绑";
        sn = StringUtils.trim(sn);
        if (opType.equals("bind")) {
            opTypeName = "绑定";
            if (StringUtils.isBlank(sn)) return RestResult.error("操作失败,缺少参数【sn】");
        } else {
            sn = "";
        }
        RestResult result=service.scanUseSN(matrCodeHis,inpatientCode,sn,opType,hisKey,outpatientNo,oprtNo);
        return result;
    }

    /**
     * @desc his视图直接查询
     */
    @GetMapping("/pageHisView")
    public RestResult pageHisView() {
        QueryContext queryContext = QueryContextHolder.currentContext();

        if (!BeforeReport.getFlagAllData()) {
            List<String> deptHisCodeList = BeforeReport.getUserDeptCodeHisList();
            ((QueryWrapper<HisReqBill>)queryContext.getWrapper()).and(
                    query ->
                            query.in("dept_code_patient", deptHisCodeList).or()
                                    .in("dept_code_op", deptHisCodeList)
            );
        }
        IPage pageResult= hisReqViewService.matrBillPage(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
